﻿/*----------------------------------------------------------------
//  <copyright file="FunctionAuthCacheRefreshEvent.cs" company="MicroCloud@151504200868">
//      Copyright © 2020-2024 MicroCloud Corporation, All rights reserved.
//  </copyright>
//  <site>https://gitee.com/chenmm123/microclouds</site>
//  <last-editor>cmm</last-editor>
//  <last-date>2023-12-27 10:08</last-date>
//----------------------------------------------------------------*/

namespace MicroCloud
{
    #region "功能权限缓存刷新事件源"
    /// <summary>
    /// 功能权限缓存刷新事件源
    /// </summary>
    public class FunctionAuthCacheRefreshEventData : EventDataBase
    {
        #region "属性"
        #region "获取或设置 功能编号"
        /// <summary>
        /// 获取或设置 功能编号
        /// </summary>
        public long[] FunctionIds { get; set; }
        #endregion
        #region "获取或设置 用户编号集合"
        /// <summary>
        /// 获取或设置 用户编号集合
        /// </summary>
        public string[] UserIds { get; set; }
        #endregion

        #endregion

        #region "构造函数"
        #region "初始化一个功能权限缓存刷新事件源的新实例"
        /// <summary>
        /// 初始化一个功能权限缓存刷新事件源 <see cref="FunctionAuthCacheRefreshEventData"/> 的新实例
        /// </summary>
        public FunctionAuthCacheRefreshEventData()
        {
            FunctionIds = Array.Empty<long>();
            UserIds = Array.Empty<string>();
        }
        #endregion

        #endregion

    }
    #endregion

    #region "功能权限缓存刷新事件处理器"
    /// <summary>
    /// 功能权限缓存刷新事件处理器
    /// </summary>
    public class FunctionAuthCacheRefreshEventHandler : EventHandlerBase<FunctionAuthCacheRefreshEventData>
    {
        //字段
        private readonly IServiceProvider _provider;

        #region "构造函数"
        #region "初始化一个功能权限缓存刷新事件处理器的新实例"
        /// <summary>
        /// 初始化一个功能权限缓存刷新事件处理器 <see cref="FunctionAuthCacheRefreshEventHandler"/> 的新实例
        /// </summary>
        /// <param name="provider"></param>
        public FunctionAuthCacheRefreshEventHandler(IServiceProvider provider)
        {
            _provider = provider;
        }
        #endregion

        #endregion

        #region "方法"
        #region "事件处理"
        /// <summary>
        /// 事件处理
        /// </summary>
        /// <param name="eventData">事件源数据</param>
        /// <param name="provider">服务提供者</param>
        public override void Handle(FunctionAuthCacheRefreshEventData eventData, IServiceProvider provider)
        {
            if (!_provider.InHttpRequest())
            {
                return;
            }
            IFunctionAuthCache cache = _provider.GetRequiredService<IFunctionAuthCache>();
            if (eventData.FunctionIds.Length > 0)
            {
                cache.RemoveFunctionCaches(eventData.FunctionIds);
                foreach (var functionId in eventData.FunctionIds)
                {
                    cache.GetFunctionRoles(functionId, _provider);
                }
            }
            if (eventData.UserIds.Length > 0)
            {
                cache.RemoveUserCaches(eventData.UserIds);
                foreach (string UserId in eventData.UserIds)
                {
                    cache.GetUserFunctions(UserId);
                }
            }
        }
        #endregion

        #endregion

    }
    #endregion

}
